﻿using UnityEngine;
using System.Collections;

public class UpgradeSystem : MonoBehaviour {
	public Unit commandCenter;
	private int attackUpCost = 50;
	public int attackUp{
		get{return attackUpCost;}
	}
	private int attackSpeedUpCost = 50;
	public int attackSpeedUp{
		get{return attackSpeedUpCost;}
	}
	private int speedUpCost = 50;
	public int speedUp{
		get{return speedUpCost;}
	}
	private int healthUpCost = 50;
	public int healthUp{
		get{return healthUpCost;}
	}
	private static float costMultiplier = 1.1f;
	public GUIText attackText;
	public GUIText attackSpeedText;
	public GUIText speedText;
	public GUIText healthText;
	public static int getCost(int lvl){
		return (int) (50 * Mathf.Pow(costMultiplier,lvl));
	}
	void Update(){
		foreach(GUIText text in GetComponentsInChildren<GUIText>())
		{
			text.enabled=commandCenter.isSelected ();
		}
		foreach(GUITexture tex in GetComponentsInChildren<GUITexture>()){
			tex.enabled=commandCenter.isSelected();
		}
		if(commandCenter.isSelected() && Input.GetButtonDown("Fire1")){
			Vector3 mouse = Input.mousePosition;
			if(mouse.y<64){
				int x = (int)(Screen.width-mouse.x);
				if(x>60 && x<120){
					if(commandCenter.owner.gold> healthUpCost && commandCenter.owner.lifeUpgrade < 50)
					{
						commandCenter.owner.lifeUpgrade++;
						commandCenter.owner.gold-=healthUpCost;
						healthUpCost = (int) (healthUpCost * costMultiplier);
						if(commandCenter.owner.lifeUpgrade < 50)
							healthText.text = "Next: " + healthUpCost;
						else healthText.text = "MAX";
					}
				}else if(x<184){
					if(commandCenter.owner.gold> speedUpCost && commandCenter.owner.speedUpgrade < 50)
					{
						commandCenter.owner.speedUpgrade++;
						commandCenter.owner.gold-=speedUpCost;
						speedUpCost = (int) (speedUpCost * costMultiplier);
						if(commandCenter.owner.speedUpgrade < 50)
							speedText.text = "Next: " + speedUpCost;
						else speedText.text = "MAX";
					}
				}else if(x<248){
					if(commandCenter.owner.gold> attackSpeedUpCost && commandCenter.owner.atkSpeedUpgrade < 50)
					{
						commandCenter.owner.atkSpeedUpgrade++;
						commandCenter.owner.gold-= attackSpeedUpCost;
						attackSpeedUpCost = (int) (attackSpeedUpCost * costMultiplier);
						if(commandCenter.owner.atkSpeedUpgrade < 50)
							attackSpeedText.text = "Next: " + attackSpeedUpCost;
						else attackSpeedText.text = "MAX";
					}
				}else if(x<312){
					if(commandCenter.owner.gold> attackUpCost && commandCenter.owner.attackUpgrade < 50)
					{
						commandCenter.owner.attackUpgrade++;
						commandCenter.owner.gold-=attackUpCost;
						attackUpCost = (int) (attackUpCost * costMultiplier);
						if(commandCenter.owner.attackUpgrade < 50)
							attackText.text = "Next: " + attackUpCost;
						else attackText.text = "MAX";
					}
				}
			}
		}
	}
}
